import { createRouter, createWebHistory } from "vue-router";
import { useAuthStore } from "../stores/auth";

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: "/",
      redirect: "/data",
    },
    {
      path: "/login",
      name: "LoginPage",
      component: () => import("../views/Login/LoginPage.vue"),
    },
    {
      path: "/logout",
      name: "LogoutPage",
      component: () => import("../views/Login/LogoutPage.vue"),
    },
    {
      path: "/data",
      name: "data",
      component: () => import("../views/DataPanel/DataOverview.vue"),
    },
    {
      path: "/FactoryAnimation",
      component: () => import("@/views/FactoryAnimation/Main.vue"),
      children: [
        {
          path: "/home",
          name: "home",
          component: () => import("@/views/FactoryAnimation/Home.vue"),
        },
        {
          path: "/index1",
          name: "index1",
          component: () => import("@/views/FactoryAnimation/index1.vue"),
        },
        {
          path: "/join",
          name: "join",
          component: () => import("@/views/FactoryAnimation/join.vue"),
        },
        {
          path: "/xx",
          name: "xx",
          component: () => import("@/views/FactoryAnimation/xx.vue"),
        },
        {
          path: "/factory",
          name: "factory",
          component: () => import("@/views/FactoryAnimation/factory.vue"),
        },
        {
          path: "/factory2",
          name: "factory2",
          component: () => import("@/views/FactoryAnimation/factory2.vue"),
        },
        {
          path: "/loading",
          name: "loading",
          component: () => import("@/views/FactoryAnimation/loading.vue"),
        },
      ],
    },
    {
      path: "/fileTable",
      name: "fileTable",
      component: () => import("@/views/FileTable/FileTable.vue"),
    },
  ],
});

router.beforeEach((to, from, next) => {
  const authStore = useAuthStore();

  if (to.meta.requiresAuth && !authStore.user) {
    next("/login");
  } else if (to.path === "/login" && authStore.user) {
    next("/");
  } else {
    next();
  }
});

export default router;
